package com.wanxi.servlet.contact;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.ContactVO;
import com.wanxi.VO.NavigationVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.ContactService;
import com.wanxi.service.NavigationService;
import com.wanxi.service.impl.ContactServiceImpl;
import com.wanxi.service.impl.NavigationServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 8:50
 */
//@WebServlet("/back/contact/add")
public class ContactAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2、获取前端传递后端的值
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String website = req.getParameter("website");
        String enable = req.getParameter("enable");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //3.封装VO数据
        ContactVO contactVO = new ContactVO();
        contactVO.setAddress(Tool.nullToStr(address));
        contactVO.setPhone(Tool.nullToStr(phone));
        contactVO.setEmail(Tool.nullToStr(email));
        contactVO.setWebsite(Tool.nullToStr(website));
        contactVO.setEnable(Tool.strToInt(enable));
        contactVO.setPage(Tool.strToPage(page));
        contactVO.setLimit(Tool.strToLimit(limit));

        // 4.调用服务逻辑层
        ContactService contactService = new ContactServiceImpl();
        //        5.得到返回的结果
        ResultDTO resultDTO = contactService.add(contactVO);
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }


}
